Skip to content

Make a couple minor changes to utils needed to migrate Google GenAi instrumentation to utils library#17

Open
DylanRussell wants to merge 3 commits into
open-telemetry:mainfrom
DylanRussell:minor_utils_change
Open

Make a couple minor changes to utils needed to migrate Google GenAi instrumentation to utils library#17
DylanRussell wants to merge 3 commits into
open-telemetry:mainfrom
DylanRussell:minor_utils_change

Conversation

@DylanRussell
Copy link
Copy Markdown
Contributor

@DylanRussell DylanRussell commented May 14, 2026

A couple minor changes needed to migrate Google GenAi over to the utils library..

Make GenAiInvocation class (and parent classes) a context manager by defining enter/exit methods.. This way it can be optionally used as a ContextManager like python's file object, and we just need 1 method (invocation) instead of 2 (start_invocaton/invocation)

Open questions:

What's the best way to deprecate these methods ? We could do a release of GenAi utils, then I could update all instrumentations to stop using these methods, then release those instrumentations, and then finally remove these methods. Or we could have a long deprecation period ? But that feels unnecessary..

Should we stop using typing.Any types on ToolInvocation and all these other classes ? IMO we should force instrumentation to serialize these to things that can go into the span/metric/log (like the OTEL Attributes type), Any is too generic to serialize.. Also IMO the sem convs themselves should be more opinionated about how to serialize things..

@DylanRussell DylanRussell requested a review from a team as a code owner May 14, 2026 20:17
Copilot AI review requested due to automatic review settings May 14, 2026 20:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the GenAI utility's invocation lifecycle so GenAIInvocation (and its subclasses) implement the context manager protocol directly via __enter__/__exit__, allowing the existing handler.inference(), handler.embedding(), handler.tool(), handler.workflow(), handler.invoke_local_agent(), and handler.invoke_remote_agent() factory methods to return the invocation object itself — usable either manually or via with. The older handler.start_* factories are kept and marked deprecated to preserve back-compat. Also threads a tool_result constructor parameter and records gen_ai.tool.call.arguments / gen_ai.tool.call.result on tool spans, and migrates all tests to the new factory API.

Changes:

  • Replace _managed() contextmanager helper with __enter__/__exit__ on GenAIInvocation; rework handler.* factories to return invocations directly (instead of AbstractContextManager); deprecate start_* factories via docstrings.
  • Add tool_result constructor parameter to ToolInvocation and emit GEN_AI_TOOL_CALL_ARGUMENTS / GEN_AI_TOOL_CALL_RESULT attributes at both start and finish.
  • Migrate test suite from handler.start_* to the new handler.* factory methods.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_invocation.py Replace _managed contextmanager with __enter__/__exit__; clean up unused imports
util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py Make inference/embedding/tool/workflow/invoke_*_agent return invocations directly; deprecate start_* factories
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py Update docstrings to point at new factory
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_embedding_invocation.py Update docstrings to point at new factory
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_workflow_invocation.py Update docstrings to point at new factory
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_agent_invocation.py Update docstrings to point at new factories
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_tool_invocation.py Add tool_result ctor param; emit tool args/result attributes
util/opentelemetry-util-genai/tests/test_utils.py Switch tests from start_inference/start_embedding to new factories
util/opentelemetry-util-genai/tests/test_utils_events.py Switch tests to handler.inference
util/opentelemetry-util-genai/tests/test_toolcall.py Switch tests to handler.tool
util/opentelemetry-util-genai/tests/test_workflow_invocation.py Switch tests to handler.workflow
util/opentelemetry-util-genai/tests/test_handler_workflow.py Switch tests to handler.workflow
util/opentelemetry-util-genai/tests/test_handler_metrics.py Switch tests to new inference/embedding/tool factories
util/opentelemetry-util-genai/tests/test_handler_completion_hook.py Switch tests to new inference/workflow/invoke_*_agent factories
util/opentelemetry-util-genai/tests/test_handler_agent.py Switch tests to handler.invoke_local_agent/invoke_remote_agent

Comment thread util/opentelemetry-util-genai/src/opentelemetry/util/genai/_invocation.py Outdated
Comment thread util/opentelemetry-util-genai/src/opentelemetry/util/genai/_tool_invocation.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants